package com.entrogy.controller;


import com.entrogy.bean.Appraisal;
import com.entrogy.bean.PageBean;
import com.entrogy.bean.ShiftEntity;
import com.entrogy.service.ShiftService;
import com.entrogy.utils.ApiReturnUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**班次管理控制层
 * @Author 许红松
 * @date 2019/5/9 0009 下午 2:56
 */
@RestController
@Transactional
@RequestMapping(value = "/shift")
@Api(description = "对班次管理信息操作的相关接口")
public class ShiftController {

    @Autowired
    private ShiftService shiftService;//班次管理信息service接口

    /**
     *   班次管理分页
     * @return List
     */
    @ApiOperation(value = "分页获取班次管理信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "学校ID",name = "school_code",dataType = "int",paramType = "query",required = true),
            @ApiImplicitParam(value = "当前页数",name="currentPage",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",dataType = "int",paramType = "query",required = true)
    })
    @RequestMapping(value = "/getShiftList",method = RequestMethod.POST)
    public PageBean<Map<String,Object>>  getShiftList(@RequestParam(value="currentPage")Integer  currentPage, @RequestParam(value="pageSize")Integer   pageSize, @RequestParam(value="school_code")Integer school_code, String shiftName,String shiftId){

        Map<String,Object> map=new HashMap<>();
        map.put("school_code",school_code);
        if ( shiftName!=null && !"".equals(shiftName)){
            map.put("shiftName",shiftName);
        }
        if ( shiftId!=null && !"".equals(shiftId)){
            map.put("shiftId",shiftId);
        }
        int total1=shiftService.Total(map);
        PageHelper.startPage(currentPage, pageSize);
        List<Map<String,Object>> list=shiftService.SelectShiftByList(map);
        return new PageBean<Map<String,Object>>().pageBeans(list,total1);
    }

    /**
     *   新增班次管理信息
     * @param school_code
     * @param shiftEntity
     * @return
     */
    @ApiOperation(value = "新增班次管理")
    @RequestMapping(value = "/insertShift",method = RequestMethod.POST)
    public Map<String,Object> insertShift(@ModelAttribute ShiftEntity shiftEntity,  @RequestParam(value="school_code")Integer school_code){
        shiftEntity.setSchool_code(school_code);
        if (shiftEntity.getShiftType()==1){//代表1天一次上下班
            //考勤内容写入该字段  易于查询
            shiftEntity.setDescribes(shiftEntity.getShift1Uptime()+"-"+shiftEntity.getShift1Downtime());
        }else if(shiftEntity.getShiftType()==2){//代表1天二次上下班
            //考勤内容写入该字段  易于查询
            shiftEntity.setDescribes(shiftEntity.getShift1Uptime()+"-"+shiftEntity.getShift1Downtime()+" "+shiftEntity.getShift2Uptime()+"-"+shiftEntity.getShift2Downtime());
        }else if(shiftEntity.getShiftType()==3){//代表1天三次上下班
            //考勤内容写入该字段  易于查询
            shiftEntity.setDescribes(shiftEntity.getShift1Uptime()+"-"+shiftEntity.getShift1Downtime()+" "+shiftEntity.getShift2Uptime()+"-"+shiftEntity.getShift2Downtime()+" "+shiftEntity.getShift3Uptime()+"-"+shiftEntity.getShift3Downtime());
        }
        int  code=shiftService.addShift(shiftEntity);
        if (code==1){
            return ApiReturnUtil.success("新增成功","");
        }else{
            return ApiReturnUtil.success("新增失败","");
        }


    }

    /**
     *   修改班次管理信息
     * @return
     */
    @ApiOperation(value = "修改班次管理")
    @RequestMapping(value = "/updateShift",method = RequestMethod.POST)
    public Map<String,Object> updateShift(@ModelAttribute ShiftEntity shiftEntity,@RequestParam(value="school_code")Integer school_code){
        shiftEntity.setSchool_code(school_code);

       if (shiftEntity.getShiftType()!=0 && !"".equals(shiftEntity.getShiftType())){
           if (shiftEntity.getShiftType()==1){//代表1天一次上下班
               //考勤内容写入该字段  易于查询
               shiftEntity.setDescribes(shiftEntity.getShift1Uptime()+"-"+shiftEntity.getShift1Downtime());
           }else if(shiftEntity.getShiftType()==2){//代表1天二次上下班
               //考勤内容写入该字段  易于查询
               shiftEntity.setDescribes(shiftEntity.getShift1Uptime()+"-"+shiftEntity.getShift1Downtime()+" "+shiftEntity.getShift2Uptime()+"-"+shiftEntity.getShift2Downtime());
           }else if(shiftEntity.getShiftType()==3){//代表1天三次上下班
               //考勤内容写入该字段  易于查询
               shiftEntity.setDescribes(shiftEntity.getShift1Uptime()+"-"+shiftEntity.getShift1Downtime()+" "+shiftEntity.getShift2Uptime()+"-"+shiftEntity.getShift2Downtime()+" "+shiftEntity.getShift3Uptime()+"-"+shiftEntity.getShift3Downtime());
           }
       }
        int  code=shiftService.UpdateShift(shiftEntity);
        if (code==1){
            return ApiReturnUtil.success("修改成功","");
        }else{
            return ApiReturnUtil.success("修改失败","");
        }


    }

    /**
     *   删除班次管理信息
     * @return
     */
    @ApiOperation(value = "删除考勤组管理信息")
    @RequestMapping(value = "/deleteShiftById",method = RequestMethod.POST)
    public Map<String,Object> deleteShiftById(@RequestParam(value="school_code")int school_code,@RequestParam(value="shiftId")int  shiftId){

        int  code= shiftService.deleteShiftById(school_code, shiftId);
        if(code==1){
            return ApiReturnUtil.success("删除成功","");
        }else if(code==2){
            return ApiReturnUtil.success("不允许删除使用中的班次","");
        }
        return ApiReturnUtil.error("删除失败");
    }

}
